const exam = {"0":'知识点A_难度1',"1":'知识点A_难度2',"2":'知识点A_难度3',"3":'知识点B_难度1',"4":'exercise 5',"5":'exercise 6'};//题库，从中选取习题

var nodeStack=[];
var currentNode=[];
var curNode=[];

window.onload = function () {
    var url = "index.json";
    var request = new XMLHttpRequest();
    request.open("get", url);
    request.send(null);
    request.onload = function () {
        // 函数实现
        if (request.status == 200) {
            var json = JSON.parse(request.responseText);
            nodeStack.push(json);
            displayNextNode()
        }
    }
}

function displayNode(node){
    var exerciseDiv=document.createElement('div');
    var choices=['正确答案','错误答案']
    for(var key in node.data.exercises){
        var ques=exam[key];
        var span=document.createElement('span');
        span.innerHTML=ques;
        var answerDiv=document.createElement('div');
        answerDiv.appendChild(span)
        
        for(var i=0;i<choices.length;++i){
            var choice=document.createElement('input')
            choice.type='radio'
            var span=document.createElement('span')
            span.innerHTML=choices[i]
            choice.className='radio_'+key;
            choice.setAttribute('data-i',i)
            choice.onchange=function (e) {
                var target=e.target;
                var choices=document.getElementsByClassName(target.className)
                var checked=target.getAttribute('data-i')
                for (var radio of choices){
                    var data_i=radio.getAttribute('data-i')
                    if(data_i==checked){
                        radio.checked=true;
                    }else{
                        radio.checked=false;
                    }
                }
            }
            answerDiv.appendChild(span)
            answerDiv.appendChild(choice)
        }
        exerciseDiv.appendChild(answerDiv)
    }

    var demo=document.getElementById('demo');
    demo.innerHTML="";
    demo.appendChild(exerciseDiv);
    var test = document.getElementById('test');
    curNode=node;
    test.addEventListener('click',onSubmit);
}

function displayNextNode(){
        currentNode=nodeStack.pop();
        if(currentNode==null){
            var demo=document.getElementById('demo');
            demo.innerHTML="做完啦"
        }else{
            displayNode(currentNode);
        }
}

function onSubmit() {
    var conditionNode = curNode.children;
    var dic={}; //记录答题正误 {题号:正确/错误,...}
    if (conditionNode.length > 0) {
        // 获取题目正误,存入字典
        for(var key in currentNode.data.exercises){
            var choices=document.getElementsByClassName('radio_'+key)
            var checked=0;
            for (var choice of choices){
                if(choice.checked){
                    checked=choice.getAttribute('data-i');
                    break;
                }
            }
            if(checked==0){
                dic[key]=true;
            }else{
                dic[key]=false;
            }
        }


        for (var i = conditionNode.length - 1; i >= 0; i--) {
            // 检查条件是否满足
            var conditions=conditionNode[i].data.conditions;
            var satisfied=true;
            for(var key in conditions){
                if(dic[key]==true){
                    satisfied=false;
                    break;
                }
            }

            if(!satisfied){
                continue;
            }

            for(var j = conditionNode[i].children.length -1;j>=0;j--) {
                nodeStack.push(conditionNode[i].children[j]);
            }
        }
    }
    displayNextNode();
}
